-module(test_ssl_server).
-export([start/0]).

-define(TCP_SEND_TIMEOUT, 15000).

%% 依赖SSL证书和privatekeypem
%% ssl.pem 是证书和privatekey的合并文件.
%%
start() ->
    {ok, Listen} = gen_tcp:listen(4567,  [binary, {packet, 0},
                                                  {active, false},
				                  {reuseaddr, true},
				                  {nodelay, true},
				                  {send_timeout, ?TCP_SEND_TIMEOUT},
				                  {keepalive, true}]),
    spawn(fun() -> accept(Listen) end).

accept(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> accept(Listen) end),
    loop(Socket).

loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
	{ok, Data} ->
	    io:format("(~p)received: ~p~n", [Socket, Data]),
	    gen_tcp:send(Socket, iolist_to_binary(["server#", Data])),
	    loop(Socket);
	{error, closed} ->
	    io:format("[~p] tcp_closed~n", [Socket])
    end.
